﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace ITXMOV
{
    public partial class MainPage:ViewBase
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        IntexReptServBLClient WCFClient
        {
            get
            {
                return app.WCFClient;
            }
        } 
        public string REMARK
        {
            get { return (string)GetValue(REMARKProperty); }
            set { SetValue(REMARKProperty, value); }
        }
        public static readonly DependencyProperty REMARKProperty =
            DependencyProperty.Register("REMARK", typeof(string), typeof(MainPage), null);

        public string MACHINEID
        {
            get { return (string)GetValue(MACHINEIDProperty); }
            set { SetValue(MACHINEIDProperty, value); }
        }
        public static readonly DependencyProperty MACHINEIDProperty =
            DependencyProperty.Register("MACHINEID", typeof(string), typeof(MainPage), null);
        public string STAFF
        {
            get { return (string)GetValue(STAFFProperty); }
            set { SetValue(STAFFProperty, value); }
        }
        public static readonly DependencyProperty STAFFProperty =
            DependencyProperty.Register("STAFF", typeof(string), typeof(MainPage), null);

        public string ABPAUF
        {
            get { return (string)GetValue(ABPAUFProperty); }
            set { SetValue(ABPAUFProperty, value); }
        }
        public static readonly DependencyProperty ABPAUFProperty =
            DependencyProperty.Register("ABPAUF", typeof(string), typeof(MainPage), null);

        public string MSG
        {
            get { return (string)GetValue(MSGProperty); }
            set { SetValue(MSGProperty, value); }
        }
        public static readonly DependencyProperty MSGProperty =
            DependencyProperty.Register("MSG", typeof(string), typeof(MainPage), null);
        

        public MainPage()
        {
            vm = new VITXMPDC();
            DataContext = vm;
            InitializeComponent();
        }
        public bool IsSetup = false;
        private void TV_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsSetup)
                return;
            IsSetup = true;
            WCFClient.GetInfoCompleted += WCFClient_GetInfoCompleted; 
        }
        void WCFClient_GetInfoCompleted(object sender, GetInfoCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                    return;
                ImageResult result = e.Result; 
                string str1 = "";
                str1 = result.str1;
                if (str1 == "RequestAgentStart")
                {
                    return;
                }
                if (result.str0 == null)
                    return;
                if (result.str0.Length < 20)
                    return; 
                XDocument xdoc = XDocument.Parse(result.str0); 
                string field = "";
                string value = "";
                foreach (XElement tables in xdoc.Elements())
                {
                    foreach (XElement rows in tables.Elements())
                    {
                        field = rows.Name.ToString();
                        value = rows.Value.ToString();
                        if (field == "ABPOS")
                        {
                            GetABP(rows);
                            break;
                        } 
                    }
                }
                MSG = result.str0;
                txtMsg.Text = MSG;
                txtAgentID.Text = AGENTID;
                //MessageBox.Show(str0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        public void GetABP(XElement row)
        {
            string field = "";
            string value = "";
            string krs = "", lfd = "", pos = "";
            foreach (XElement columns in row.Elements())
            {
                field = columns.Name.ToString();
                value = columns.Value.ToString();
                if (field == "ABPAUFKRS")
                {
                    krs = value;
                }
                if (field == "ABPAUFLFD")
                {
                    lfd = value;
                }
                if (field == "ABPAUFPOS")
                {
                    pos = value;
                }
            }
            ABPAUF = krs + lfd + pos;
        }
        private void Check(object sender, RoutedEventArgs e)
        {
            string CommandString = "SELECT * FROM ABPOS WHERE ROWNUM &lt; 2";
            string dbuser = "intex";
            string intex = "intex1";
            string dbpwd = "intex";// "intexdbadmin";
            string OracleDB = "user id=" + dbuser + ";data source=" + intex + ";password=" + dbpwd + ";Unicode=True;" + "Persist Security Info=True;"; ;
            string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                   "<QueryBuilder>" +
                   "<AGENTID>" + AGENTID + "</AGENTID>" +
                   "<DBTYPE>ORACLE</DBTYPE>" +
                   "<DataTable>ABPOS</DataTable>" +
                   "<ConnectionString>" + OracleDB + "</ConnectionString>" +
                   "<CommandType>Text</CommandType>" +
                   "<CommandText>" + CommandString + "</CommandText>" +
                   "</QueryBuilder>"; 
            if (WCFClient.State == System.ServiceModel.CommunicationState.Created
                || WCFClient.State == System.ServiceModel.CommunicationState.Opened)
            {

                WCFClient.GetInfoAsync(rawMessage);
            }

        } 
        private void LogIn(object sender, RoutedEventArgs e)
        {
            app.GetID();
        }
        private void Quit(object sender, RoutedEventArgs e)
        {
            app.QAGENT();
        }
        private void AUF_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void ABPAUF_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void MACHINEIDF_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void STAFF_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void REMARK_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

        } 
    }
}
